﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using UnityEngine;
using System.Text.RegularExpressions;

public class EmojiTranslate
{
    static Dictionary<string, string> emojiCode;
    static void MakeEmojiTranslate()
    {
        emojiCode = new Dictionary<string, string>();
        string emojiInfo = AppFacade.resourceManager.LoadText("Res/Internal/Prefab/ui/Common/InlineText/EmojiTexture/info.txt");
        string[] lines = emojiInfo.Split('\n');
        for(int i = 0; i < lines.Length; i++)
        {
            string[] splits = lines[i].Split(' ');
            if (splits.Length < 2)
                continue;
            string emojicode = splits[1].TrimEnd('\r');
            emojiCode[GetConvertedString(emojicode)] = emojicode;
        }
    }

    private static string GetConvertedString(string inputString)
    {
        string[] converted = inputString.Split('-');
        for (int j = 0; j < converted.Length; j++)
        {
            converted[j] = char.ConvertFromUtf32(Convert.ToInt32(converted[j], 16));
        }
        string convertedString = string.Join(string.Empty, converted);

        //Debug.Log(convertedString + " " + inputString +" " + converted.Length.ToString());
        return convertedString;
    }

    private static string ReplaceEmoji(string input)
    {
        return string.Format("[#{0}]", emojiCode[input]);
    }

    static string Utf32SubString(string inputString, int startIndex, int len)
    {
        int realLen = 0;
        for(int i = 0; i < len; i++, realLen++)
        {
            if (startIndex + realLen >= inputString.Length)
                return null;

            try
            {
                int utf32CodePoint = char.ConvertToUtf32(inputString, startIndex + realLen);
                if (utf32CodePoint > 0xffff)
                {
                    realLen++;
                }

            }
            catch(Exception)
            {
                return null;
            }
        }

        return inputString.Substring(startIndex, realLen);
    }


    //注意emoji要保持在SpriteAssets的第一位置
    public static string Translate(string inputString)
    {
        if(emojiCode == null)
            MakeEmojiTranslate();

        StringBuilder sb = new StringBuilder();

        int i = 0;
        while (i < inputString.Length)
        {
            string singleChar = null;
            string doubleChar = null;
            string threeChar = null;
            string fourChar = null;
            string fiveChar = null;
            string sixChar = null;
            string sevenChar = null;
            string eightChar = null;

            if ((eightChar = Utf32SubString(inputString, i, 8)) != null && emojiCode.ContainsKey(eightChar))
            {
                sb.Append(ReplaceEmoji(eightChar));
                i += eightChar.Length;
            }
            else if ((sevenChar = Utf32SubString(inputString, i, 7)) != null && emojiCode.ContainsKey(sevenChar))
            {
                sb.Append(ReplaceEmoji(sevenChar));
                i += sevenChar.Length;
            }
            else if ((sixChar = Utf32SubString(inputString, i, 6)) != null && emojiCode.ContainsKey(sixChar))
            {
                sb.Append(ReplaceEmoji(sixChar));
                i += sixChar.Length;
            }
            else if ((fiveChar = Utf32SubString(inputString, i, 5)) != null && emojiCode.ContainsKey(fiveChar))
            {
                sb.Append(ReplaceEmoji(fiveChar));
                i += fiveChar.Length;
            }
            else if ((fourChar = Utf32SubString(inputString, i, 4)) != null && emojiCode.ContainsKey(fourChar))
            {
                sb.Append(ReplaceEmoji(fourChar));
                i += fourChar.Length;
            }
            else if ((threeChar = Utf32SubString(inputString, i, 3)) != null && emojiCode.ContainsKey(threeChar))
            {
                sb.Append(ReplaceEmoji(threeChar));
                i += threeChar.Length;
            }
            else if ((doubleChar = Utf32SubString(inputString, i, 2)) != null && emojiCode.ContainsKey(doubleChar))
            {
                sb.Append(ReplaceEmoji(doubleChar));
                i += doubleChar.Length;
            }
            else if ((singleChar = Utf32SubString(inputString, i, 1)) != null && emojiCode.ContainsKey(singleChar))
            {
                sb.Append(ReplaceEmoji(singleChar));
                i += singleChar.Length;
            }
            else
            {
                sb.Append(inputString[i]);
                i++;
            }
        }

        return sb.ToString();
    }

    public static char CheckIsUnicode(char inputString)
    {
        var pattern= @"\p{Cs}";
        string s = string.Format("{0}", inputString);
        if (Regex.IsMatch(s, pattern))
        {
            return  '\0';
        }
        else
        {
            return inputString;
        }
    }
}
